package com.gitee.xqxyxchy.crypto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 参数对象
 *
 */
public class CryptoParam implements Serializable {

	private static final long serialVersionUID = 1L;
	private String key;
	private String value;

	public CryptoParam() {
		super();
	}

	public CryptoParam(String key, String value) {
		super();
		this.key = key;
		this.value = value;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public static class Builder {

		private List<CryptoParam> params;

		private Builder() {
			params = new ArrayList<>();
		}

		public static Builder create() {
			return new Builder();
		}

		public Builder add(String key, String value) {
			params.add(new CryptoParam(key, value));
			return this;
		}

		public String get(String key, CryptoParam... params) {
			if (Objects.nonNull(params) && params.length > 0) {
				for (CryptoParam param : params) {
					if (Objects.isNull(param.getKey()) || param.getKey().trim().equals("")
							|| Objects.isNull(param.getValue())) {
						continue;// key为空或者值为null跳过替换
					}
					if (param.getKey().equals(key)) {
						return param.getValue();
					}
				}
			}
			return null;
		}

		public String get(String key, String defaultValue, CryptoParam... params) {
			if (Objects.nonNull(params) && params.length > 0) {
				for (CryptoParam param : params) {
					if (Objects.isNull(param.getKey()) || param.getKey().trim().equals("")
							|| Objects.isNull(param.getValue())) {
						continue;// key为空或者值为null跳过替换
					}
					if (param.getKey().equals(key)) {
						return param.getValue();
					}
				}
			}
			return defaultValue;
		}
		
		public CryptoParam[] build() {
			return array();
		}

		public CryptoParam[] array() {
			return params.toArray(new CryptoParam[0]);
		}

		public List<CryptoParam> list() {
			return params;
		}

	}

}
